;(function(root,factory){
	if (typeof define === 'function' && define.amd) {
    define(factory);
  } else if (typeof exports === 'object') {
    module.exports = factory();
  } else {
    root.Aline = factory();
  }
})(this,function(){
	function Aline(option) {
		this.el = option.el; // 绑定元素
		this.duration = option.duration || 1000; // 基本动画速度
		this.delay = option.delay || 0; // 动画初始延迟
		this.playtime = 0; // 播放时长
		var elDom = document.querySelector(this.el);
		this.init = function () {
			var self = this;
			var playtime = 0;
			var paths = elDom.querySelectorAll('path'); // 获取绑定的元素中所有的path元素
			// 获取第一个path的长度
			var firstPathLength = Math.ceil(paths[0].getTotalLength());
			var lastPath = paths[paths.length - 1];
			new Array().forEach.call(paths, function (v, i) {
				// 获取图形长度
				var pathLength = Math.ceil(v.getTotalLength());
				// 动态设置svg属性
				v.style.strokeDashoffset = pathLength;
				v.style.strokeDasharray = pathLength;
				v.style.fillOpacity = 0;
				playtime += pathLength / firstPathLength * self.duration;
				// 动态设置每一个path动画播放速度
				v.style.animationDuration = pathLength / firstPathLength * self.duration + 'ms';
				var delayLength = 0;
				for (var j = 0; j < i; j++) {
					delayLength += Math.ceil(paths[j].getTotalLength());
				}
				// 动态设置每一个path的动画延迟时间
				v.style.animationDelay = (delayLength / firstPathLength * self.duration + self.delay) +
					'ms';
			});
			lastPath.addEventListener('webkitAnimationEnd', function () {
				elDom.classList.remove('infinite');
			})
			this.playtime = playtime;
		}
		this.infinite = function () {
			elDom.classList.add('infinite');
		}
		this.replay = function () {
			elDom.classList.remove('normal');
			setTimeout(function () {
				elDom.classList.add('normal');
			}, 100)
		}
		if (elDom.classList.contains('infinite')) {
			setInterval(this.infinite, this.playtime);
		} else {
			elDom.classList.add('normal');
		}
		this.init.call(this);
	}
	return Aline;
})

